package com.pennysaver.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pennysaver.adaptor.UserAdaptor;

/*
 * Servlet implementation class RegisterUser
 RegisterUser?userName=user1&firstName=barack&lastName=obama&emailAddress=obama@america.com&birthYear=1964&birthMonth=08&birthDate=04
 */

@WebServlet("/RegisterUser")
public class RegisterUser extends PennysaverServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see PennysaverServlet#PennysaverServlet()
	 */
	public RegisterUser() {
		super();
	}

	@Override
	protected void defaultMethod(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		UserAdaptor adaptor = new UserAdaptor();
		String userName = request.getParameter("userName");
		String firstName = request.getParameter("firstName");
		String lastName = request.getParameter("lastName");
		String emailAddress = request.getParameter("emailAddress");
		String birthYear = request.getParameter("birthYear");
		String birthMonth = request.getParameter("birthMonth");
		String birthDate = request.getParameter("birthDate");

		if (adaptor.registerUser(userName, firstName, lastName, emailAddress,
				birthDate, birthMonth, birthYear)) {
			response.getWriter()
					.append("[{\"error\" : \"false\", \"message\" :  \"User Succesfully registered\"}]");
		} else {
			response.getWriter()
					.append("[{\"error\": \"false\", \"message\" :  \"Error Creating User.\"}");
		}

	}

	@Override
	protected boolean validateInput(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		return true;
	}

}
